﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;

namespace Cloudin.AppsRss.Apps.CSDN
{
    /// <summary>
    /// 采集新闻类
    /// </summary>
    public class News
    {
        /// <summary>
        /// 根据采集链接地址获取XML内容
        /// </summary>
        public static string GetXMLByUrl()
        {
            return CreateArticleXML("http://articles.csdn.net/plus/list.php?tid=1008");
        }

        /// <summary>
        /// 生成XML文件
        /// </summary>
        /// <param name="strWebUrl">链接地址</param>
        public static string CreateArticleXML(string strWebUrl)
        {
            string strReturnValue = "";
            string strGetHtmlCode = "";
            string strXML = "";
            strXML = strXML + "<?xml version=\"1.0\" encoding=\"utf-8\" ?> ";
            strXML = strXML + "<rss version=\"2.0\">";
            strXML = strXML + "<channel>";
            strXML = strXML + "<title>Cloudin</title>";

            try
            {
                Cloudin.Utility.WebClient wc = new Cloudin.Utility.WebClient();
                wc.Encoding = System.Text.Encoding.GetEncoding("utf-8");
                wc.OpenRead(strWebUrl);
                strGetHtmlCode = wc.RespHtml;
            }
            catch (Exception ex)
            {
                strReturnValue = "采集文章页面失败:" + ex.Message;
            }

            //匹配列表
            try
            {
                Regex rx = new Regex("</h6>(?<content>[\\s\\S]*)?<div class=\"page\">", RegexOptions.Singleline);
                string strGetListHtmlContent = rx.Match(strGetHtmlCode).Result("${content}");
                strGetListHtmlContent = strGetListHtmlContent.Replace("</div>", "|");
                string[] splitContent = strGetListHtmlContent.Split(new char[] { '|' });
                for (int i = 0; i < splitContent.Length; i++)
                {
                    string strTitle = "";
                    string strUrl = "";
                    string strDate = "";
                    string strImage = "";
                    string strContent = "";
                    string strSource = "";
                    string strAuthor = "";
                    string strValue = splitContent[i];

                    if (i >= 0)
                    {
                        try
                        {
                            //匹配链接
                            try
                            {
                                Regex rxLink = new Regex("<dt class=\"txt_blue txt_14\"><a href=\"(?<content>[\\s\\S]*)?\" class=\"title\">", RegexOptions.Singleline);
                                strUrl = rxLink.Match(strValue).Result("${content}");
                            }
                            catch (Exception ex)
                            {
                                strReturnValue = "链接匹配失败:" + ex.Message;
                            }

                            //标题
                            try
                            {
                                Regex rxTitle = new Regex("\" class=\"title\">(?<content>[\\s\\S]*)?</a><span>", RegexOptions.Singleline);
                                strTitle = rxTitle.Match(strValue).Result("${content}");
                                strTitle = Cloudin.Utility.StringHelper.CleanHtml(strTitle);
                            }
                            catch (Exception ex)
                            {
                                strReturnValue = "标题匹配失败:" + ex.Message;
                            }

                            //日期
                            try
                            {
                                Regex rxDate = new Regex("<span>(?<content>[\\s\\S]*)?</span>", RegexOptions.Singleline);
                                strDate = rxDate.Match(strValue).Result("${content}");
                            }
                            catch (Exception ex)
                            {
                                strReturnValue = "日期匹配失败:" + ex.Message;
                            }

                            //图片
                            try
                            {
                                Regex rxImage = new Regex("<img src=\"(?<content>[\\s\\S]*)?\" alt=\"", RegexOptions.Singleline);
                                strImage = rxImage.Match(strValue).Result("${content}");
                            }
                            catch (Exception ex)
                            {
                                strReturnValue = "图片匹配失败:" + ex.Message;
                            }

                            //匹配内容
                            try
                            {
                                Cloudin.Utility.WebClient wc2 = new Cloudin.Utility.WebClient();
                                wc2.Encoding = System.Text.Encoding.GetEncoding("utf-8");
                                wc2.OpenRead(strUrl);
                                string strGetContentHtml = wc2.RespHtml;

                                Regex rx2 = new Regex("<div class=\"blkCont\" style=\"margin:0 40px;\">(?<content>[\\s\\S]*)?<div class=\"page\" id=\"arc_page\">", RegexOptions.Singleline);
                                string strShowContent = rx2.Match(strGetContentHtml).Result("${content}");
                                strShowContent = strShowContent.Replace("</p>", "{#p}");
                                strShowContent = strShowContent.Replace("</P>", "{#p}");
                                strShowContent = strShowContent.Replace("<br>", "{#br}");
                                strShowContent = strShowContent.Replace("<BR>", "{#br}");
                                strShowContent = strShowContent.Replace("<br />", "{#br}");
                                strShowContent = strShowContent.Replace("<BR />", "{#br}");
                                strShowContent = strShowContent.Replace("<br/>", "{#br}");
                                strShowContent = strShowContent.Replace("<BR/>", "{#br}");
                                strShowContent = Cloudin.Utility.StringHelper.ClearATag(strShowContent);
                                strShowContent = Cloudin.Utility.StringHelper.CleanHtml(strShowContent);
                                strShowContent = Cloudin.Utility.StringHelper.ReplaceASCII(strShowContent);
                                strShowContent = strShowContent.Replace("\t", "");
                                strShowContent = strShowContent.Replace("\r", "");
                                strShowContent = strShowContent.Replace("\n", "");
                                strShowContent = strShowContent.Replace("{#p}", "\r\n\r\n");
                                strShowContent = strShowContent.Replace("{#br}", "\r\n");
                                strContent = strShowContent;

                                Regex rxArea = new Regex("<p class=\"blueline\">(?<content>[\\s\\S]*)?<span><cite>", RegexOptions.Singleline);
                                string strAreaHTML = rxArea.Match(strGetContentHtml).Result("${content}");

                                //匹配来源
                                try
                                {
                                    Regex rxSource = new Regex("<span>来源：(?<content>[\\s\\S]*)?【已有", RegexOptions.Singleline);
                                    strSource = rxSource.Match(strAreaHTML).Result("${content}");
                                    strSource = Cloudin.Utility.StringHelper.CleanHtml(strSource);
                                    strSource = strSource.Replace("\t", "");
                                    strSource = strSource.Replace("\r", "");
                                    strSource = strSource.Replace("\n", "");
                                    strSource = strSource.Replace("  ", "");
                                }
                                catch (Exception ex)
                                {
                                    strReturnValue = "来源匹配失败:" + ex.Message;
                                }

                                //作者
                                try
                                {
                                    Regex rxAuthor = new Regex("<span>作者：(?<content>[\\s\\S]*)?</span>", RegexOptions.Singleline);
                                    strAuthor = rxAuthor.Match(strAreaHTML).Result("${content}");
                                }
                                catch (Exception ex)
                                {
                                    strReturnValue = "作者匹配失败:" + ex.Message;
                                }
                            }
                            catch (Exception ex)
                            {
                                string strError = ex.Message;
                            }

                            if (!string.IsNullOrEmpty(strContent))
                            {
                                strXML = strXML + "<item>\r\n";
                                strXML = strXML + "<title><![CDATA[" + strTitle + "]]></title>\r\n";
                                strXML = strXML + "<link>" + strUrl + "</link>\r\n";
                                strXML = strXML + "<description><![CDATA[" + strContent + "]]></description>\r\n";
                                strXML = strXML + "<category>业界</category>\r\n";
                                strXML = strXML + "<pubDate>" + strDate + "</pubDate>\r\n";
                                strXML = strXML + "<image>" + strImage + "</image>\r\n";
                                strXML = strXML + "<author>" + strAuthor + "</author>\r\n";
                                strXML = strXML + "<comments>" + strSource + "</comments>\r\n";
                                strXML = strXML + "</item>\r\n";
                            }
                        }
                        catch (Exception ex)
                        {
                            strReturnValue = "内容匹配失败:" + ex.Message;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                strReturnValue = "文章列表匹配失败:" + ex.Message;
            }

            try
            {
                strXML = strXML + "</channel>";
                strXML = strXML + "</rss>";
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.LoadXml(strXML);
                xmlDoc.Save(System.Windows.Forms.Application.StartupPath + "\\csdn\\news.xml");
            }
            catch (Exception ex)
            {
                strReturnValue = "生成XML文件失败:" + ex.Message;
            }

            return strReturnValue;
        }
    }
}
